package com.kll.carolinehmibiz.net

import android.annotation.SuppressLint
import android.content.Context
import androidx.core.util.Preconditions
import com.kll.carolinehmibiz.base.net.UrlConfig
import com.kll.carolinehmibiz.base.utils.L
import com.kll.carolinehmibiz.model.AccountModel
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Request


/**
 * ====================================================
 * Created by Ziliang Cai on 2021/12/6 22:58
 * <a href="mailto:752777456@qq.com">Contact me</a>
 * ====================================================
 */
class RetrofixUrlManager {

    private lateinit var mInterceptor: Interceptor
    private var mContext: Context? = null


    private constructor(context: Context) {

        mContext = context.applicationContext
        mInterceptor = Interceptor {
            it.proceed(processRequset(it.request()))
        }
    }

    companion object {
        var mInstance: RetrofixUrlManager? = null

        @Synchronized
        fun getInstance(context: Context): RetrofixUrlManager {
            if(mInstance == null) {
                mInstance = RetrofixUrlManager(context)
            }

            return mInstance!!
        }
    }

    @SuppressLint("RestrictedApi")
    fun with(build: OkHttpClient.Builder): OkHttpClient.Builder {
        Preconditions.checkNotNull(build, "builder cannot be null")
        return build.addInterceptor(mInterceptor)
    }

    private fun processRequset(request: Request?): Request? {

        L.d("RetrofixUrlManager", "processRequset:: request is ${if(request == null) "null" else "not null"}")

        if(request == null) return request

//        var newBuilder = request.newBuilder()

//        val oldRequest: Request = chain.request()
//        if(Globals.isDebug){
//            L.e(">>>Request url", oldRequest.url().uri().getPath());
//        }
        //        if(Globals.isDebug){
//            L.e(">>>Request url", oldRequest.url().uri().getPath());
//        }

        val authorizedUrlBuilder = request.url().newBuilder().scheme(request.url().scheme()).host(request.url().host())

// 添加公共参数

        // 添加公共参数
//        val params: Map<String, String> =
//            SdkContext.getSdkContext().getHttpRequestConfig().getCommonParams()
//        if (params != null) {
//            val keys = params.keys
//            for (key in keys) {
//                authorizedUrlBuilder.addQueryParameter(key, params[key])
//            }
//        }

        val builder: Request.Builder = request.newBuilder()
            .method(request.method(), request.body())
            .url(authorizedUrlBuilder.build())

        // 添加公共头部
        // 从sdkContext中获取用户设置>>>Authorization的全局头部信息

        builder.addHeader(UrlConfig.HEADER_APP_TYPE, "2")
      //  builder.addHeader(UrlConfig.HEADER_SN, "${AccountModel.getInstance(mContext!!).getSn()}")
        builder.addHeader(UrlConfig.HEADER_SN, "${AccountModel.SN}")
        builder.addHeader(UrlConfig.HEADER_VERSION, "1.0.0")
       // L.e("-----","-------获取到的设备编码是"+"${AccountModel.SN}")
        var token = AccountModel.getInstance(mContext!!).getUserToken()
        if(token != null) {
            builder.addHeader(UrlConfig.HEADER_TOKEN, token.token)
        }

        //
//        builder.addHeader("Accept-Encoding", "identity")

        return builder.build()
    }
}